package com.example.myspringbootpj.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.myspringbootpj.entity.Word;
import com.example.myspringbootpj.handler.MyTextTypeHandler;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface WordMapper extends BaseMapper<Word> {

    @Select("""
SELECT word_id, word_text
FROM word
LIMIT #{offset}, #{limit}
""")
    @Results({
            @Result(column = "word_id", property = "word_id"),
            @Result(column = "word_text", property = "word_text")
    })
    List<Word> getWordsWithOffset(@Param("offset") int offset, @Param("limit") int limit);


    @Insert("""
    INSERT INTO new_study (user_id, word_id, study_num)
    SELECT #{userId} AS user_id, word_id, 0 AS study_num
    FROM word
    WHERE word_id NOT IN (        SELECT word_id FROM new_study WHERE user_id = #{userId}
    )
    ORDER BY word_id
    LIMIT 20
    """)
    void insertNewStudyBatch(@Param("userId") String userId);

    @Update("""
UPDATE new_study
SET study_num = study_num + 1
WHERE user_id = #{userId} AND word_id = #{wordId};
""")
    void insertStudyNum(@Param("userId") String userId, @Param("wordId") Integer wordId);


    @Update("""
INSERT INTO review_study (user_id, word_id, study_num)
SELECT user_id, word_id, study_num
FROM new_study
WHERE user_id = #{userId} AND word_id = #{wordId} AND study_num >= 3
""")
    void moveToReviewStudy(@Param("userId") String userId, @Param("wordId") Integer wordId);

//查询背过的单词
    @Select("""
    SELECT
        w.word_text
    FROM
        review_study rw
    INNER JOIN
        word w
    ON
        rw.word_id = w.word_id
    WHERE
        rw.user_id = #{userId}
""")
    List<String> getReviews(@Param("userId") String userId);

//insert into collection (user_id, word_id) SET user_id=#{userId},word_id=#{wordId}

    //收藏单词
    @Insert("""
 INSERT INTO collection (user_id, word_id) VALUES (#{userId}, #{wordId})""")
    void insertCollection(@Param("userId") String userId, @Param("wordId") Integer wordId);

    //获取收藏
    @Select("""
    SELECT w.word_id, w.word_text 
    FROM collection cw 
    INNER JOIN word w ON cw.word_id = w.word_id 
    WHERE cw.user_id = #{userId}
""")
    @Results({
            @Result(column = "word_id", property = "word_id"),
            @Result(column = "word_text", property = "word_text")
    })
    List<Word> getCollection(@Param("userId") String userId);



    //获得单词总数
    @Select("""
select count(*) from word""")
    int getWordCount();
//    // 使用 MyBatis-Plus 分页查询单词列表
//    default List<Word> getWords(int limit) {
//        Page<Word> page = new Page<>(1, limit); // 获取第一页，每页 limit 条记录
//        return this.selectPage(page, new QueryWrapper<>()).getRecords(); // 查询并返回记录
//    }
}


//    @Insert("INSERT INTO word (word_text) VALUES (#{word_text})")
//    @Options(useGeneratedKeys = true, keyProperty = "word_id")
//    void insertWord(Word word);

